package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.PermissionException;
import com.itheima.exception.UserException;
import com.itheima.pojo.Permission;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        List<Permission> permissions = permissionMapper.findPage(queryPageBean.getQueryString());

        return PageResult.builder().rows(page.getResult()).total(page.getTotal()).build();
    }

    @Override
    public boolean add(Permission permission) {
        int res = permissionMapper.add(permission);

        return res == 1;

    }

    @Override
    public void deleteById(Integer id) throws PermissionException {
        int count = permissionMapper.findConutById(id);

        if(count>0){
            throw new PermissionException("存在引用 不能删除");
        }else {
            permissionMapper.deleteById(id);
        }
    }

    @Override
    public Permission findById(String id) {
        Permission permission = permissionMapper.findById(id);

        return permission;
    }

    @Override
    public boolean edit(Permission permission) {
        int res = permissionMapper.edit(permission);

        return res == 0 || res == 1;
    }

    @Override
    public List<Permission> findall() {

        List<Permission> permissions = permissionMapper.findall();

        return permissions;
    }
}
